View Javadoc

1   /*
2    * Copyright (c) 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * StringParamHelper.java
6    *
7    * Created on 17-Dec-2004 at 11:48:09 AM
8    */
9   package ca.uhn.cache.internal.hibernate.impl;
10  
11  import java.text.MessageFormat;
12  import java.util.HashMap;
13  import java.util.Iterator;
14  import java.util.Map;
15  import java.util.Set;
16  
17  import ca.uhn.cache.IGroupQueryParam;
18  import ca.uhn.cache.IPointQueryParam;
19  import ca.uhn.cache.impl.StringParam;
20  import ca.uhn.cache.impl.StringSetParam;
21  import ca.uhn.cache.internal.hibernate.IQueryParamHelper;
22  
23  
24  /***
25   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
26   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:52:40 $ by $Author: bryan_tripp $
27   */
28  public class StringParamHelper implements IQueryParamHelper {
29  
30      /***
31       */
32      public StringParamHelper() {
33      }
34  
35      /***
36       * {@inheritDoc}
37       */
38      public Field createField( Record theRecord, IPointQueryParam theQueryParam ) {
39          assert theQueryParam instanceof StringParam;
40          
41          return new StringParamField( theRecord, (StringParam) theQueryParam );
42      }
43      
44      /***
45       * {@inheritDoc}
46       */
47      public Class getFieldClass() {
48          return StringParamField.class;
49      }
50      
51      /***
52       * {@inheritDoc}
53       */
54      public String generateHql( IPointQueryParam thePointQueryParam, int theIndex ) {
55          assert thePointQueryParam instanceof StringParam;
56          
57          return MessageFormat.format( 
58                  "( f.record = f{0}.record AND f{0}.dimensionName = :f{0}dimensionName AND f{0}.value = :f{0}value )", 
59                  new Object[] { new Integer(theIndex) } );
60      }
61  
62      /***
63       * {@inheritDoc}
64       */
65      public Map generateHqlParamMap( IPointQueryParam thePointQueryParam, int theIndex ) {
66          assert thePointQueryParam instanceof StringParam;
67          
68          Map retVal = new HashMap();
69          
70          StringParam sp = (StringParam) thePointQueryParam;
71          
72          retVal.put( 
73                  MessageFormat.format( "f{0}dimensionName", new Object[] { new Integer( theIndex ) } ), 
74                  sp.getDimension().getName() );
75          
76          retVal.put( 
77                  MessageFormat.format( "f{0}value", new Object[] { new Integer( theIndex ) } ), 
78                  sp.getValue() );
79          
80          return retVal;
81      }
82  
83      /***
84       * {@inheritDoc}
85       */
86      public String generateHql( IGroupQueryParam theGroupQueryParam, int theIndex ) {
87          assert theGroupQueryParam instanceof StringSetParam;
88          
89          StringSetParam ssp = (StringSetParam) theGroupQueryParam;
90  
91          StringBuffer inExprValues = new StringBuffer();
92          
93          int sspValueIndex = 1;
94          Set values = ssp.getValues();
95          for (Iterator iter = values.iterator(); iter.hasNext();) {
96              String value = (String) iter.next();
97              
98              if ( sspValueIndex > 1 ) {
99                  inExprValues.append( ", " );
100             }
101             
102             inExprValues.append( 
103                     MessageFormat.format( 
104                             ":f{0}value{1}", 
105                             new Object[] { new Integer(theIndex), new Integer( sspValueIndex++ ) } ) );
106         }
107         
108         return MessageFormat.format( 
109                 "( f.record = f{0}.record " +
110                 "AND f{0}.dimensionName = :f{0}dimensionName " +
111                 "AND f{0}.value IN ( {1} ) )", 
112                 new Object[] { new Integer(theIndex), inExprValues.toString() }  );
113     }
114 
115     /***
116      * {@inheritDoc}
117      */
118     public Map generateHqlParamMap( IGroupQueryParam theGroupQueryParam, int theIndex ) {
119         //TODO make sure the values of the StringSetParam are sorted
120         assert theGroupQueryParam instanceof StringSetParam;
121         
122         Map retVal = new HashMap();
123         
124         StringSetParam ssp = (StringSetParam) theGroupQueryParam;
125         
126         retVal.put( 
127                 MessageFormat.format( "f{0}dimensionName", new Object[] { new Integer( theIndex ) } ), 
128                 ssp.getDimension().getName() );
129 
130         int sspValueIndex = 1;
131         Set values = ssp.getValues();
132         for (Iterator iter = values.iterator(); iter.hasNext();) {
133             String value = (String) iter.next();
134             retVal.put( 
135                     MessageFormat.format( 
136                             "f{0}value{1}", 
137                             new Object[] { new Integer( theIndex), new Integer( sspValueIndex++ ) } ), 
138                     value );
139         }
140         
141         return retVal;
142     }
143 
144 }